/**
 * 
 */
package np.com.abhishek.scrambler.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

/**
 * @author Abhishek
 * 
 */

/*
 * frmSizeSelector.java this class is used to display the boardSelector dialog
 * box i.e. allows takes the user's specification of the boards type (Image/
 * Number) and the size ( > 1) and also contains two buttons the OK button to
 * confirm the specification and thus starts the game and the CANCEL button is
 * used to quit the dialog
 */

public class SizeSelector extends DialogFrame implements ActionListener,
		ItemListener {

	private static final long serialVersionUID = 5L;

	JPanel pnlSizeSelector, pnlBoardSelector;

	JButton btnOK, btnCancel;

	ButtonGroup boardType;

	JRadioButton radNum, radImg;

	JTextField txtSize;

	JLabel lblSize;

	JButton btn[];

	public static int size;

	private static int type;

	final static int NUMBER = 0;

	final static int IMAGE = 1;

	public SizeSelector(JFrame owner) {
		super(owner, "Size Selector", true, 200, 210, null);

		pnlSizeSelector = new JPanel();
		pnlBoardSelector = new JPanel();

		pnlSizeSelector.setLayout(null);
		pnlBoardSelector.setLayout(null);

		pnlSizeSelector.setBorder(new javax.swing.border.TitledBorder(
				"Select Size:"));
		pnlBoardSelector.setBorder(new javax.swing.border.TitledBorder(
				"Select Type:"));

		pnlBoardSelector.setBounds(5, 5, 190, 75);
		pnlSizeSelector.setBounds(5, 80, 190, 50);

		add(pnlSizeSelector);
		add(pnlBoardSelector);

		boardType = new ButtonGroup();

		radNum = new JRadioButton("Number Scrambler");
		radImg = new JRadioButton("Image Scrambler");

		pnlBoardSelector.setLayout(null);
		radNum.setBounds(10, 20, 175, 20);
		radNum.doClick();
		type = NUMBER;
		radImg.setBounds(10, 45, 175, 20);

		radNum.addItemListener(this);
		radImg.addItemListener(this);

		pnlBoardSelector.add(radNum);
		pnlBoardSelector.add(radImg);

		boardType.add(radNum);
		boardType.add(radImg);

		lblSize = new JLabel("Size Of The Board:");
		txtSize = new JTextField();
		btnOK = new JButton("OK");
		btnCancel = new JButton("Cancel");

		lblSize.setBounds(15, 20, 120, 20);
		txtSize.setBounds(140, 20, 40, 20);

		btnOK.setBounds(15, 135, 75, 25);
		btnCancel.setBounds(115, 135, 75, 25);

		pnlSizeSelector.add(lblSize);
		pnlSizeSelector.add(txtSize);

		btnOK.addActionListener(this);
		btnCancel.addActionListener(this);

		add(btnOK);
		add(btnCancel);

		getRootPane().setDefaultButton(btnOK);
		setSize(210, 200);
		setLayout(null);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setVisible(true);
	}

	public void actionPerformed(ActionEvent evt) {
		int tempSize = 0;

		if (evt.getSource() == btnCancel)
			setVisible(false);

		else if (evt.getSource() == btnOK) {
			// to catch if the user supplied value is text
			try {
				tempSize = Integer.parseInt(txtSize.getText());
			} catch (NumberFormatException e) {
			}

			if (tempSize < 2) { // checking if the number entered is less
				// than 2 entering 1 is useless
				JOptionPane.showMessageDialog(null,
						"Please choose size greater than 1!",
						"Scrambler - Invalid Board Size",
						JOptionPane.INFORMATION_MESSAGE);
				txtSize.setText("");
			} else { // this is the valid case i.e. the used supplied number
				// is > 2 and thus playable
				size = tempSize;
				setVisible(false);
				try {
					// dispose the old window
					Initializer.playWindow.dispose();
					// System.out.println("Old window disposed.");
				} catch (Exception e) {
					// System.out.println("No window has been opened.");
					/*
					 * initially no frame is loaded, so it catches that
					 * exception
					 */
				}
				/*
				 * once the board size and type has been specified by the use we
				 * can draw the board and that is done by Initializer class
				 */
				Initializer board = new Initializer(size, type);
				board.drawBoard(); // places the buttons on the board
				board.findHtNfreePt(); // finds the height of the button
				// and the free slot
			}
		}
	}

	public void itemStateChanged(ItemEvent i) {
		if (i.getSource() == radNum) // inherited from
			// java.util.EventObject
			type = NUMBER;

		if (i.getItemSelectable() == radImg) // own method
			type = IMAGE;
	}
}
